package Viewer;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import DataModel.Tupel;

/**
 * This class represents the menu where all visual settings of the track can be done
 * @author Stefan Lorenz<br>
 * <br>29.11.2012
 */
@SuppressWarnings("serial")
public class SettingsMenu extends JFrame {

    /**
     * Viewer of the GeneScapes session
     */
    private Viewer viewer;
    /**
     * The panel where the menu widgets are placed on
     */
    private JPanel settingsWindow;

    public SettingsMenu(Viewer viewer) {
	this.setIconImage(new ImageIcon(SettingsMenu.class.getResource("../resources/export32.png")).getImage());
	this.setLayout(new BorderLayout());
	this.settingsWindow = new JPanel();
	this.add(this.settingsWindow, BorderLayout.CENTER);

	this.viewer = viewer;
	this.pack();
    }

    /**
     * Opens the window to set size color look atc.<br>
     * 
     * @param selectedMenu
     * <br>
     *            describes the Tupel whichs defines the Track<br>
     *            0: Track & i number of track<br>
     *            1: Wiggle & i number of track<br>
     *            2: SixFrameTranslation & i number of track<br>
     *            3: DNA Sequence & i number of track<br>
     *            4: general color settings...choosenItem must be 2
     *            5: general size settings...choosenItem must be 2
     * 
     * @param choosenItem
     * <br>
     *            0 = size<br>
     *            1 = look<br>
     *            2 = color<br>
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void openSettings(final Tupel selectedMenu, int choosenItem) {
	String name = "";
	settingsWindow.removeAll();
	settingsWindow.revalidate();

	JPanel panel = new JPanel();
	this.settingsWindow.add(panel, BorderLayout.CENTER);

	// now fill panel with widgets
	if (selectedMenu.getStart() == 0) {
	    if (this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("gbk") ||
	    		this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("embl") ||
	    		this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("gff") || 
	    		this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("sam") ||
	    		this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("gtf") ||
	    		this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("bed")) {

		final JComboBox featureBox = new JComboBox();
		featureBox.addItem("gene");
		featureBox.addItem("mRNA");
		featureBox.addItem("CDS");
		featureBox.addItem("Other features");

		if (this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("gff") || this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("sam") || this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("gtf")
			|| this.viewer.getLoadedTracks()[selectedMenu.getStop()].getTypeOfFile().contains("bed")) {
		    featureBox.setSelectedIndex(3);
		}

		// size
		if (choosenItem == 0) {
		    panel.setLayout(new GridLayout(5, 2));
		    name += "Size: Track " + selectedMenu.getStop();

		    final JSpinner featureLabelSize = new JSpinner();
		    featureLabelSize.setPreferredSize(new Dimension(50, 25));
		    featureLabelSize.setValue(this.viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getLabelSize());

		    final JSpinner sizeSpinner = new JSpinner();
		    sizeSpinner.setPreferredSize(new Dimension(50, 25));
		    sizeSpinner.setValue(this.viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getSize(featureBox.getSelectedIndex()));

		    final JSpinner space = new JSpinner();
		    space.setPreferredSize(new Dimension(50, 25));
		    space.setValue(this.viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getSpace());

		    final JSpinner bucketSpace = new JSpinner();
		    bucketSpace.setPreferredSize(new Dimension(50, 25));
		    bucketSpace.setValue(this.viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getBucketSpace());

		    ChangeListener cl = new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setLabelSize(Integer.parseInt(featureLabelSize.getValue().toString()));
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setSize(Integer.parseInt(sizeSpinner.getValue().toString()), featureBox.getSelectedIndex());
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setSpace(Integer.parseInt(space.getValue().toString()));
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setBucketSpace(Integer.parseInt(bucketSpace.getValue().toString()));
			    viewer.getDrawPanel().repaint();
			}
		    };
		    featureLabelSize.addChangeListener(cl);
		    sizeSpinner.addChangeListener(cl);
		    space.addChangeListener(cl);
		    bucketSpace.addChangeListener(cl);

		    panel.add(new JLabel("Bucket Label:"));
		    panel.add(featureLabelSize);
		    panel.add(new JLabel("Feature Type:"));
		    panel.add(featureBox);
		    panel.add(new JLabel("Size:"));
		    panel.add(sizeSpinner);
		    panel.add(new JLabel("Feature Space:"));
		    panel.add(space);
		    panel.add(new JLabel("Bucket Space:"));
		    panel.add(bucketSpace);

		    featureBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    space.setValue(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getSpace());
			    featureLabelSize.setValue(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getLabelSize());
			    sizeSpinner.setValue(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getSize(featureBox.getSelectedIndex()));

			}
		    });
		}
		// look
		else if (choosenItem == 1) {
		    name += "Look: Track " + selectedMenu.getStop();
		    panel.setLayout(new GridLayout(3, 2));
		    
		    
		    final JComboBox lookBox = new JComboBox(new Object[] {"0", "1", "2", "  3", "  4"});
		   		    
		    Map<Object, Icon> icons = new HashMap<Object, Icon>();
		    icons.put("0",new ImageIcon(SettingsMenu.class.getResource("/resources/look-combo_rounded-rectangle.png")));
		    icons.put("1",new ImageIcon(SettingsMenu.class.getResource("../resources/look-combo_arrow.png")));
		    icons.put("2",new ImageIcon(SettingsMenu.class.getResource("../resources/look-combo_peaked-rectangle.png")));
		    icons.put("  3",new ImageIcon(SettingsMenu.class.getResource("../resources/look-combo_rectangle.png")));
		    icons.put("  4",new ImageIcon(SettingsMenu.class.getResource("../resources/look-combo_heatfeat.png")));
		    
		    lookBox.setRenderer(new IconListRenderer(icons));
		    lookBox.setSelectedIndex(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getLook(featureBox.getSelectedIndex()));

		    final JCheckBox show = new JCheckBox();
		    show.setSelected(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getFeatureShown(featureBox.getSelectedIndex()));

		    ActionListener ac = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setLook(lookBox.getSelectedIndex(), featureBox.getSelectedIndex());
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setFeatureShown(show.isSelected(), featureBox.getSelectedIndex());
			    
			    
			    if(!viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getFeatureShown(0) &&
				    !viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getFeatureShown(1) &&
				    !viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getFeatureShown(2) &&
				    !viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getFeatureShown(3))
				viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setTrackShown(false);
			    else
				viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setTrackShown(true);
			    
			    
			    
			    viewer.getDrawPanel().repaint();
			}
		    };
		    lookBox.addActionListener(ac);
		    show.addActionListener(ac);

		    panel.add(new JLabel("Feature Type:"));
		    panel.add(featureBox);
		    panel.add(new JLabel("Show:"));
		    panel.add(show);
		    panel.add(new JLabel("Look:"));
		    panel.add(lookBox);

		    featureBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    show.setSelected(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getFeatureShown(featureBox.getSelectedIndex()));
			    lookBox.setSelectedIndex(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getLook(featureBox.getSelectedIndex()));
			
			}
		    });
		}
		// color
		else if (choosenItem == 2) {
		    panel.setLayout(new BorderLayout());
		    name += "Color: Track " + selectedMenu.getStop();
		    final JColorChooser cc = new JColorChooser(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getColor(featureBox.getSelectedIndex()));

		    JButton apply = new JButton("Apply");
		    apply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setColor(cc.getColor(), featureBox.getSelectedIndex());
			    viewer.getDrawPanel().repaint();
			}
		    });

		    JPanel panel2 = new JPanel(new GridBagLayout());
		    GridBagConstraints gc = new GridBagConstraints();
		    gc.fill = GridBagConstraints.NONE;
		    gc.gridx = 0;
		    panel2.add(new JLabel("Feature Type:"), gc);
		    gc.gridx = 1;
		    panel2.add(featureBox, gc);

		    JPanel panel3 = new JPanel(new BorderLayout());
		    panel3.add(panel2, BorderLayout.WEST);
		    panel3.add(apply, BorderLayout.EAST);

		    panel.add(cc, BorderLayout.CENTER);
		    panel.add(panel3, BorderLayout.SOUTH);

		    featureBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    cc.setColor(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getColor(featureBox.getSelectedIndex()));
			}
		    });

		}
	    }
	}
	if (selectedMenu.getStart() == 1) {
	    if (choosenItem == 0) {
		panel.setLayout(new GridLayout(3, 2));
		name += "Size: Wiggle-Plot " + selectedMenu.getStop();

		SpinnerModel smScale = new SpinnerNumberModel(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getWiggleScaleSize(), 0, 100, 1);
		final JSpinner scaleSizeSpinner = new JSpinner(smScale);

		SpinnerModel smFrameSize = new SpinnerNumberModel(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getWiggleFrameSize(), 0, 100, 1);
		final JSpinner frameSizeSpinner = new JSpinner(smFrameSize);

		SpinnerModel sm = new SpinnerNumberModel(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getSizeOfReadWiggle(), 0, 100, 1);
		final JSpinner sizeSpinner = new JSpinner(sm);

		sizeSpinner.setPreferredSize(new Dimension(50, 25));
		scaleSizeSpinner.setPreferredSize(new Dimension(50, 25));
		ChangeListener cl = new ChangeListener() {
		    public void stateChanged(ChangeEvent e) {
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setWiggleFrameSize(Integer.parseInt(frameSizeSpinner.getValue().toString()));
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setWiggleScaleSize(Integer.parseInt(scaleSizeSpinner.getValue().toString()));
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setSizeOfReadWiggle(Integer.parseInt(sizeSpinner.getValue().toString()));
			viewer.getDrawPanel().repaint();
		    }
		};
		sizeSpinner.addChangeListener(cl);
		scaleSizeSpinner.addChangeListener(cl);
		frameSizeSpinner.addChangeListener(cl);
		panel.add(new JLabel("Size:"));
		panel.add(sizeSpinner);
		panel.add(new JLabel("Scale Size:"));
		panel.add(scaleSizeSpinner);
		panel.add(new JLabel("Frame Size:"));
		panel.add(frameSizeSpinner);

	    } else if (choosenItem == 1) {
		panel.setLayout(new GridLayout(5, 2));
		name += "Look: Wiggle-Plot " + selectedMenu.getStop();
		final JCheckBox show = new JCheckBox();
		show.setSelected(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].isReadWiggleShown());
		ActionListener ac = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setReadWiggleShown(show.isSelected());
			viewer.getDrawPanel().repaint();
		    }
		};
		show.addActionListener(ac);
		panel.add(new JLabel("Show:"));
		panel.add(show);

		final JCheckBox upsidedown = new JCheckBox();
		upsidedown.setSelected(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].isUpsidedownWiggle());
		ActionListener acupsidedown = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setUpsidedownWiggle(upsidedown.isSelected());
			viewer.getDrawPanel().repaint();
		    }
		};
		upsidedown.addActionListener(acupsidedown);
		panel.add(new JLabel("Upside down:"));
		panel.add(upsidedown);

		final JCheckBox dashed = new JCheckBox();
		dashed.setSelected(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].isDashedWiggle());
		ActionListener acdashed = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setDashedWiggle(dashed.isSelected());
			viewer.getDrawPanel().repaint();
		    }
		};
		dashed.addActionListener(acdashed);
		panel.add(new JLabel("Dashed:"));
		panel.add(dashed);

		final JCheckBox filled = new JCheckBox();
		filled.setSelected(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].isFilledWiggle());
		ActionListener acfilled = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setFilledWiggle(filled.isSelected());
			viewer.getDrawPanel().repaint();
		    }
		};
		filled.addActionListener(acfilled);
		panel.add(new JLabel("Filled:"));
		panel.add(filled);

		SpinnerModel sm = new SpinnerNumberModel(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getTransparencyWiggle() * 100, 0, 100, 1);
		final JSpinner transparency = new JSpinner(sm);
		ChangeListener transparencyac = new ChangeListener() {
		    @Override
		    public void stateChanged(ChangeEvent e) {
			viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setTransparencyWiggle(Float.parseFloat(transparency.getValue().toString()) / 100);
			viewer.getDrawPanel().repaint();
		    }
		};
		transparency.addChangeListener(transparencyac);
		panel.add(new JLabel("Transparency:"));
		panel.add(transparency);
	    } else if (choosenItem == 2) {
		name += "Color: Wiggle-Plot " + selectedMenu.getStop();
		final JComboBox wiggleColors = new JComboBox();
		wiggleColors.addItem("Wiggle Area");
		wiggleColors.addItem("Wiggle Frame");

		final JColorChooser cc = new JColorChooser(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getColorOfWiggle());
		panel.add(cc);
		JButton apply = new JButton("Apply");
		apply.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			if (wiggleColors.getSelectedIndex() == 0) {
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setColorOfWiggle(cc.getColor());
			} else if (wiggleColors.getSelectedIndex() == 1) {
			    viewer.getSettings().getTracksettings()[selectedMenu.getStop()].setColorOfWiggleFrame(cc.getColor());
			}
			viewer.getDrawPanel().repaint();
		    }
		});
		wiggleColors.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent e) {
			if (wiggleColors.getSelectedIndex() == 0) {
			    cc.setColor(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getColorOfWiggle());
			} else if (wiggleColors.getSelectedIndex() == 1) {
			    cc.setColor(viewer.getSettings().getTracksettings()[selectedMenu.getStop()].getColorOfWiggleFrame());
			}
		    }
		});

		panel.setLayout(new BorderLayout());
		JPanel panel2 = new JPanel(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill = GridBagConstraints.NONE;
		gc.gridx = 0;
		panel2.add(new JLabel("Select:"), gc);
		gc.gridx = 1;
		panel2.add(wiggleColors, gc);

		JPanel panel3 = new JPanel(new BorderLayout());
		panel3.add(panel2, BorderLayout.WEST);
		panel3.add(apply, BorderLayout.EAST);

		panel.add(cc, BorderLayout.CENTER);
		panel.add(panel3, BorderLayout.SOUTH);
	    }
	}
	if (selectedMenu.getStart() == 2) {
	    if (choosenItem == 0) {
		name += "Size: Six-Frame-Translation";
		panel.add(new JLabel("No functions yet...!"));
	    } else if (choosenItem == 1) {
		panel.setLayout(new GridLayout(1, 2));
		name += "Look: Six-Frame-Translation";
		final JCheckBox show = new JCheckBox();
		show.setSelected(viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].isSixFrameShown());
		ActionListener ac = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].setSixFrameShown(show.isSelected());
			viewer.getDrawPanel().repaint();
		    }
		};
		show.addActionListener(ac);
		panel.add(new JLabel("Show:"));
		panel.add(show);
	    } else if (choosenItem == 2) {
		panel.setLayout(new GridLayout(1, 1));
		name += "Color: Six-Frame-Translation";

		final JComboBox dna = new JComboBox();
		for (int i = 0; i < viewer.getAminoAcidTable().getStartCodons().size(); i++) {
		    dna.addItem("Start: "+viewer.getAminoAcidTable().getStartCodons().get(i)+ " +");
		}
		for (int i = 0; i < viewer.getAminoAcidTable().getStartCodons().size(); i++) {
		    dna.addItem("Start: "+viewer.getAminoAcidTable().getStartCodons().get(i)+ " -");
		}
		for (int i = 0; i < viewer.getAminoAcidTable().getStopCodons().size(); i++) {
		    dna.addItem("Stop: "+viewer.getAminoAcidTable().getStopCodons().get(i)+ " +");
		}
		for (int i = 0; i < viewer.getAminoAcidTable().getStopCodons().size(); i++) {
		    dna.addItem("Stop: "+viewer.getAminoAcidTable().getStopCodons().get(i)+ " -");
		}

//		if (selectedMenu.getStop() == 0)
//		    name += "Color: DNA-Sequence (+)";
//		else if (selectedMenu.getStop() == 1)
//		    name += "Color: DNA-Sequence (-)";
		
		final JColorChooser cc = new JColorChooser(viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].getStartCodonColor().get(0));
		dna.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent e) {
			int start = viewer.getAminoAcidTable().getStartCodons().size()*2;
			int stop = start +  viewer.getAminoAcidTable().getStopCodons().size()*2;
			
			//set start codons 
			if(dna.getSelectedIndex() < start)
			    cc.setColor(viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].getStartCodonColor().get(dna.getSelectedIndex()));
			//set stop codons 
			else if(dna.getSelectedIndex() < stop)
			    cc.setColor(viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].getStopCodonColor().get(dna.getSelectedIndex()-start));
		    }
		});

		JButton apply = new JButton("Apply");
		apply.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			int start = viewer.getAminoAcidTable().getStartCodons().size()*2;
			int stop = start +  viewer.getAminoAcidTable().getStopCodons().size()*2;
			
			//set start codons 
			if(dna.getSelectedIndex() < start)
			    viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].getStartCodonColor().set(dna.getSelectedIndex(),cc.getColor());
			//set stop codons 
			else if(dna.getSelectedIndex() < stop)
			    viewer.getSettings().getSixFrameSettings()[selectedMenu.getStop()].getStopCodonColor().set(dna.getSelectedIndex()-start,cc.getColor());
		    
			viewer.getDrawPanel().repaint();
		    }
		});
		panel.setLayout(new BorderLayout());
		JPanel panel2 = new JPanel(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill = GridBagConstraints.NONE;
		gc.gridx = 0;
		panel2.add(new JLabel("Select:"), gc);
		gc.gridx = 1;
		panel2.add(dna, gc);

		JPanel panel3 = new JPanel(new BorderLayout());
		panel3.add(panel2, BorderLayout.WEST);
		panel3.add(apply, BorderLayout.EAST);

		panel.add(cc, BorderLayout.CENTER);
		panel.add(panel3, BorderLayout.SOUTH);
	    }
	} else if (selectedMenu.getStart() == 3 || selectedMenu.getStart() == -3) {
	    if (choosenItem == 0) {
		panel.setLayout(new GridLayout(1, 2));
		if (selectedMenu.getStart() == 3)
		    name += "Size: DNA-Sequence (+)";
		else if (selectedMenu.getStart() == -3)
		    name += "Size: DNA-Sequence (-)";
		final JSpinner sizeSpinner = new JSpinner();
		sizeSpinner.setPreferredSize(new Dimension(50, 25));
		sizeSpinner.setValue(viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).getSize());
		ChangeListener cl = new ChangeListener() {
		    public void stateChanged(ChangeEvent e) {
			viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).setSize(Integer.parseInt(sizeSpinner.getValue().toString()));
			viewer.getDrawPanel().repaint();
		    }
		};
		sizeSpinner.addChangeListener(cl);

		panel.add(new JLabel("Size:"));
		panel.add(sizeSpinner);
	    } else if (choosenItem == 1) {
		if (selectedMenu.getStart() == 3)
		    name += "Look: DNA-Sequence (+)";
		else if (selectedMenu.getStart() == -3)
		    name += "Look: DNA-Sequence (-)";
		panel.add(new JLabel("No functions yet...!"));
	    } else if (choosenItem == 2) {
		final JComboBox dna = new JComboBox();
		dna.addItem("A-Background");
		dna.addItem("T-Background");
		dna.addItem("G-Background");
		dna.addItem("C-Background");
		dna.addItem("N-Background");
		dna.addItem("Foreground");

		if (selectedMenu.getStart() == 3)
		    name += "Color: DNA-Sequence (+)";
		else if (selectedMenu.getStart() == -3)
		    name += "Color: DNA-Sequence (-)";
		final JColorChooser cc = new JColorChooser(viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).getNucleotideBackgroundColor(dna.getSelectedIndex()));
		dna.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent e) {
			if (dna.getSelectedIndex() == 5)
			    cc.setColor(viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).getNucleotideForegroundColor());
			else
			    cc.setColor(viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).getNucleotideBackgroundColor(dna.getSelectedIndex()));
		    }
		});

		JButton apply = new JButton("Apply");
		apply.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			if (dna.getSelectedIndex() == 5) {
			    viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).setNucleotideForegroundColor(cc.getColor());
			} else {
			    viewer.getSettings().getSequenceSettings(selectedMenu.getStop()).setNucleotideBackgroundColor(cc.getColor(), dna.getSelectedIndex());
			}
			viewer.getDrawPanel().repaint();
		    }
		});
		panel.setLayout(new BorderLayout());
		JPanel panel2 = new JPanel(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill = GridBagConstraints.NONE;
		gc.gridx = 0;
		panel2.add(new JLabel("Select:"), gc);
		gc.gridx = 1;
		panel2.add(dna, gc);

		JPanel panel3 = new JPanel(new BorderLayout());
		panel3.add(panel2, BorderLayout.WEST);
		panel3.add(apply, BorderLayout.EAST);

		panel.add(cc, BorderLayout.CENTER);
		panel.add(panel3, BorderLayout.SOUTH);

	    }
	} else if (selectedMenu.getStart() == 4) {
	    if (choosenItem == 2) {
		final JComboBox combo = new JComboBox();
		combo.addItem("Background");
		combo.addItem("Labeling");
		combo.addItem("Selection");
		combo.addItem("Motif Search 1");
		combo.addItem("Motif Search 2");
		combo.addItem("Motif Search 3");

		name += "General Color Settings";

		final JColorChooser cc = new JColorChooser(viewer.getSettings().getBackgroundColor());
		combo.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent e) {

			if (combo.getSelectedIndex() == 0)
			    cc.setColor(viewer.getSettings().getBackgroundColor());
			else if (combo.getSelectedIndex() == 1)
			    cc.setColor(viewer.getSettings().getLabelingColor());
			else if (combo.getSelectedIndex() == 2)
			    cc.setColor(viewer.getSettings().getSelectionColor());
			else if (combo.getSelectedIndex() == 3)
			    cc.setColor(viewer.getSettings().getMotifColor(0));
			else if (combo.getSelectedIndex() == 4)
			    cc.setColor(viewer.getSettings().getMotifColor(1));
			else if (combo.getSelectedIndex() == 5)
			    cc.setColor(viewer.getSettings().getMotifColor(2));
		    }
		});

		
		JButton apply = new JButton("Apply");
		apply.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			if (combo.getSelectedIndex() == 0)
			    viewer.getSettings().setBackgroundColor(cc.getColor());
			else if (combo.getSelectedIndex() == 1)
			    viewer.getSettings().setLabelingColor(cc.getColor());
			else if (combo.getSelectedIndex() == 2)
			    viewer.getSettings().setSelectionColor(cc.getColor());
			else if (combo.getSelectedIndex() == 3)
			    viewer.getSettings().setMotifColor(cc.getColor(), 0);
			else if (combo.getSelectedIndex() == 4)
			    viewer.getSettings().setMotifColor(cc.getColor(), 1);
			else if (combo.getSelectedIndex() == 5)
			    viewer.getSettings().setMotifColor(cc.getColor(), 2);

			viewer.getDrawPanel().repaint();
		    }
		});
		panel.setLayout(new BorderLayout());
		JPanel panel2 = new JPanel(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill = GridBagConstraints.NONE;
		gc.gridx = 0;
		panel2.add(new JLabel("Select:"), gc);
		gc.gridx = 1;
		panel2.add(combo, gc);

		JPanel panel3 = new JPanel(new BorderLayout());
		panel3.add(panel2, BorderLayout.WEST);
		panel3.add(apply, BorderLayout.EAST);

		panel.add(cc, BorderLayout.CENTER);
		panel.add(panel3, BorderLayout.SOUTH);
	    }

	}
	 else if (selectedMenu.getStart() == 5) {
		    if (choosenItem == 2) {
			final JComboBox combo = new JComboBox();
			combo.addItem("Position Label Size");

			name += "General Size Settings";

			final JSpinner cc = new JSpinner();
			cc.setPreferredSize(new Dimension(50, 25));
			combo.setSelectedIndex(0);
			cc.setValue(viewer.getSettings().getPositionLabelSize());
			
			combo.addActionListener(new ActionListener() {
			    @Override
			    public void actionPerformed(ActionEvent e) {

				if (combo.getSelectedIndex() == 0)
				    cc.setValue(viewer.getSettings().getPositionLabelSize());
			    }
			});

			
			ChangeListener cl = new ChangeListener() {
			    @Override
			    public void stateChanged(ChangeEvent e) {
				viewer.getSettings().setPositionLabelSize(Integer.parseInt(cc.getValue().toString()));
				viewer.getDrawPanel().repaint();
			    }
			};
			
			cc.addChangeListener(cl);
			
			panel.setLayout(new GridLayout(1,2));
			panel.add(new JLabel("Position Label Size:"));
			panel.add(cc);

		    }

		}
	panel.revalidate();

	this.setTitle(name);
	this.setLocationRelativeTo(viewer);
	this.pack();
	panel.repaint();
	this.setVisible(true);
    }

}

/**
 * Class to set icons to the menu.<br>
 * See:<a href="http://helpdesk.objects.com.au/java/how-do-i-display-a-jcombobox-with-icons-and-text">http://helpdesk.objects.com.au/java/how-do-i-display-a-jcombobox-with-icons-and-text</a>
 * <br>29.11.2012
 */
@SuppressWarnings("serial")
class IconListRenderer extends DefaultListCellRenderer {
 
	private Map<Object, Icon> icons = null;
 
	public IconListRenderer(Map<Object, Icon> icons) {
		this.icons = icons;
	}
 
	@SuppressWarnings("rawtypes")
	@Override
	public Component getListCellRendererComponent(
			JList list, Object value, int index,
			boolean isSelected, boolean cellHasFocus) {
 
		// Get the renderer component from parent class
		JLabel label = (JLabel) super.getListCellRendererComponent(list,value, index, isSelected, cellHasFocus);
		// Get icon to use for the list item value
		Icon icon = icons.get(value);
		// Set icon to display for value
		label.setIcon(icon);
		return label;
	}
}
